package com.hoyue.dao.impl;

import com.hoyue.bean.WorkDisplay;
import com.hoyue.dao.PageDao;
import com.hoyue.dao.WorkDisplayDao;
import com.hoyue.utils.C3P0Utils;
import com.hoyue.utils.Page;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class WorkDisplayDaoImpl extends PageDaoImpl<WorkDisplay> implements WorkDisplayDao {
    QueryRunner qr = C3P0Utils.getQueryRunner();
    @Override
    public void update(WorkDisplay workDisplay) throws SQLException {
        String sql = " UPDATE work_display SET category = ?,picPath = ?,name = ? WHERE id = ? ";
        qr.update(sql,workDisplay.getCategory(),workDisplay.getPicPath(),workDisplay.getName(),workDisplay.getId());
    }

    @Override
    public List<WorkDisplay> getAllList() throws SQLException {
        String sql = " SELECT * FROM work_display ";
        return qr.query(sql, new BeanListHandler<>(WorkDisplay.class));
    }

    @Override
    public void addWorkDisplay(WorkDisplay workDisplay) throws SQLException {
        String sql = "INSERT INTO work_display (category, picPath, name) VALUES (?,?,?)";

        Object[] params = new Object[3];

        params[0] = workDisplay.getCategory();
        params[1] = workDisplay.getPicPath();
        params[2] = workDisplay.getName();
        qr.update(sql, params);
    }

    @Override
    public void deleteWorkDisplay(int id) throws SQLException {
        String sql = "DELETE FROM work_display where id=?";

        qr.update(sql,id);
    }

    @Override
    public String getPicPathById(int id) throws SQLException {
        String sql = "SELECT picPath FROM work_display WHERE id=?";

        return qr.query(sql,new ScalarHandler<>(),id);
    }

    public Page<WorkDisplay> getPage(int page,String tableName) throws SQLException {
        Page<WorkDisplay> works =  getListByPage(page,tableName);

        return works;
    }
}
